package com.lb.uts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.uts.entity.ActiveClient;
import com.lb.uts.mapper.ActiveClientMapper;
import com.lb.uts.service.AbstractBaseService;
import com.lb.uts.service.IActiveClientService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 执行器相关操作
 *
 * @Author liangb
 * @Date 2019/4/28 11:13
 */
@Service
public class ActiveClientService extends AbstractBaseService<ActiveClientMapper, ActiveClient> implements IActiveClientService {

    @Autowired
    private ActiveClientMapper activeClientMapper;

    /*
    分页查询
     */
    public PageInfo<ActiveClient> queryBySystemName(String systenName, int pageNo, int pageSize) {
        systenName = StringUtils.isEmpty(systenName) ? null : systenName;
        PageHelper.startPage(pageNo, pageSize);
        ActiveClient activeClient = new ActiveClient();
        activeClient.setSystemName(systenName);
        List<ActiveClient> scheduleJobList = queryByEntity(activeClient);
        PageInfo<ActiveClient> page = new PageInfo<>(scheduleJobList);
        return page;
    }

    /**
     * 查询执行器信息
     * system : ip1,ip2
     */
    @Override
    public List<Map<String, String>> groupBySystemName() {
        return activeClientMapper.groupBySystemName();
    }

    /**
     * 查询执行是否注册过
     */
    public int count(String ip, String systemName) {
        ActiveClient activeClient = new ActiveClient();
        activeClient.setIp(ip);
        activeClient.setSystemName(systemName);
        return activeClientMapper.selectCount(new QueryWrapper<>(activeClient));
    }

    @Override
    public int deleteBySystemNameAndIp(String systemName, String ip) {
        return activeClientMapper.deleteBySystemNameAndIp(systemName, ip);
    }
}
